package cn.itcast.service.impl;

import cn.itcast.dao.OrderListDao;
import cn.itcast.dao.OrderSettingDao;
import cn.itcast.entity.OrderQueryPageBean;
import cn.itcast.entity.PageResult;
import cn.itcast.pojo.Order;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.pojo.Params;
import cn.itcast.pojo.Setmeal;
import cn.itcast.service.OrderService;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderListDao orderListDao;

    @Autowired
    private OrderSettingDao orderSettingDao;

    @Autowired
    private RedisTemplate<String, String> redisTemplate;

    //分页查询
    @Override
    public PageResult findByPage(OrderQueryPageBean orderQueryPageBean) {
        //设置分页
        PageHelper.startPage(orderQueryPageBean.getCurrentPage(), orderQueryPageBean.getPageSize());
        List<Order> list = orderListDao.findByPage(orderQueryPageBean);
        List<Order> orderList=new ArrayList<>();
        for (Order order : list) {
            if(order.getSetmealId()!=null){
                Setmeal setmeal=orderListDao.findSetmeal(order.getSetmealId());
                order.setSetmeal(setmeal);
                order.setSetmealName(setmeal.getName());
                orderList.add(order);
            }else{
                orderList.add(order);
            }

        }
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);

        return new PageResult((long) list.size(), pageInfo.getList());
    }

    //编辑的回显，根据预约id查询订单
    @Override
    public Order findById(Long id) {
        return orderListDao.findById(id);
    }

    //编辑新增预约信息
    @Override
    public int updateOrder(Order order) {
        Params param = new Params();
        //设置时间
        DateFormat fmt =new SimpleDateFormat("yyyy-MM-dd");
        String date = fmt.format(order.getOrderDate());

        //如果有预约信息
        if (StringUtils.isNotEmpty(order.getId())) {
            return orderListDao.update(order);
        }
        //自动生成id
        //order.setId(UUID.randomUUID().toString().replace("-","").substring(0,7));
        String key = "ORDER_ID";

        order.setId(String.valueOf(redisTemplate.opsForValue().increment("ORDER_ID")));

        //查询是否有当天预约设置
        OrderSetting orderSetting = orderSettingDao.findByDate(date);

        //如果没有预约信息
        if (orderSetting == null) {
            return 0;
        }


        int old = orderListDao.insert(order);
        //查询当日预约人数
        Integer count = orderSettingDao.findOrder(date);
        param.setOrderDate(date);
        param.setReservations(count);

        //修改预约设置
        orderSettingDao.updateReservations(param);

        return old;
    }

    //修改是否到诊
    @Override
    public void changeOrderStatus(Long id, Integer arrival) {
        orderListDao.changeOrderStatus(id, arrival);
    }

    //手机端根据用户id查询用户所有预约
    @Override
    public List<Order> findUserAll(Long id) {
        return orderListDao.findByMemberId(id);
    }
}
